import objectAssign from "object-assign";
import {isServer} from "../../tools/utils";

const KEY = "DeviceInfoKey";

const initData = {};

const getValue = () => {
	let objStr = isServer ? '' : sessionStorage.getItem(KEY);
	let obj = objStr ? JSON.parse(objStr) : initData;
	return obj;
};

const saveValue = obj => {
	let objStr = JSON.stringify(obj);
	sessionStorage.setItem(KEY, objStr);
};

const deviceInfoReducer = (state = getValue(), action) => {
	switch (action.type) {
		case "device_info": {
			let _newDeviceInfo = objectAssign({}, state, action.data);
			saveValue(_newDeviceInfo);
			return _newDeviceInfo;
		}
		default:
			return getValue();
	}
};

export default deviceInfoReducer;
